﻿using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Effects;
using System.Windows.Media;
using System.Windows;

namespace CommunityToolkitMvvmDemo.Behaviors
{
 
    public class DragBehavior : Behavior<FrameworkElement>
    {
        protected bool isDragging = false;
        private Point startPosition;
        private DropShadowEffect effect = new DropShadowEffect();

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseDown += AssociatedObject_MouseDown;
            AssociatedObject.MouseMove += AssociatedObject_MouseMove;
            AssociatedObject.MouseUp += AssociatedObject_MouseUp;

            AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
            AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;

            effect.ShadowDepth = 0;
            effect.BlurRadius = 5;
            effect.Color = Colors.Black;
            AssociatedObject.Effect = effect;
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
            AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
            AssociatedObject.MouseUp -= AssociatedObject_MouseUp;
        }

        private void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isDragging = true;
            var parent = AssociatedObject.Parent as FrameworkElement;
            var clickPosition = e.GetPosition(parent);

            //首次按下时，判断RenderTransform的类型是否为TranslateTransform
            if (!(AssociatedObject.RenderTransform is TranslateTransform))
            {
                AssociatedObject.RenderTransform = new TranslateTransform();
            }

            var transform = AssociatedObject.RenderTransform as TranslateTransform;
            startPosition.X = clickPosition.X - transform.X;
            startPosition.Y = clickPosition.Y - transform.Y;
            AssociatedObject.CaptureMouse();//强制鼠标捕捉
        }
        private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isDragging)
            {
                var parent = AssociatedObject.Parent as FrameworkElement;
                Point currentPosition = e.GetPosition(parent);
                var transform = AssociatedObject.RenderTransform as TranslateTransform;

                transform.X = currentPosition.X - startPosition.X;
                transform.Y = currentPosition.Y - startPosition.Y;
            }
        }
        private void AssociatedObject_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            isDragging = false;
            AssociatedObject.ReleaseMouseCapture();//释放鼠标捕捉
        }
        //鼠标离开去掉阴影
        private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            effect.BlurRadius = 5;
        }
        //鼠标进入增加阴影
        private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            effect.BlurRadius = 15;
        }
    }

}
